
// history:createWebHistory
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

import Home from "../views/Home.vue";
// console.log(import.meta.env.BASE_URL)
const routes: Array<RouteRecordRaw> = [
  {
    path: "/student",
    name: "student",
    component: () => import("../views/Student.vue"),
  },
  {
    path: "/login",
    name: "login",

    component: () => import("../views/LoginView.vue"),
  },
  {
    path: "/admin",
    name: "admin",
    component: () => import("../views/Admin.vue"),
  },
  {
    path: "/grade",
    name: "grade",
    component: () => import("../views/Grade.vue"),
  },  {
    path: "/editescore",
    name: "editescore",
    component: () => import("../views/EditScore.vue"),
  },
  {
    path: "/allscore",
    name: "allasore",
    component: () => import("../views/AllScore.vue"),
  },
  {
    path: "/scoreinfo",
    name: "scoreinfo",
    component: () => import("../views/ScoreInfo.vue"),
  },
  {
    path: "/",
    name: "Home",
    component: Home,
  },
];

const router = createRouter({
  // history模式
  // history: createWebHistory(import.meta.env.BASE_URL),
  //hash模式 vite 的方式 vite吧  history: createWebHashHistory(process.env.BASE_URL), 中process禁用了
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes,
});
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.getItem("token") ? true : false;
  if (to.path == "/login") {
    next();
  } else {
    // 是否在登录状态下
    isLogin ? next() : next("/login");
  }
});

export default router;
